home *** CD-ROM | disk | FTP | other *** search
/ Programming Languages Suite / ProgramD2.iso / Borland / Borland C++ V5.02 / BINTREES.PAK / BINTREE.CPP next >
C/C++ Source or Header  |  1997-05-06  |  2KB  |  84 lines

  1. // ---------------------------------------------------------------------------
  2. // Copyright (C) 1994 Borland International
  3. // bintree.cpp
  4. //    Must link with myclass.cpp.
  5. // ---------------------------------------------------------------------------
  6.  
  7. #include <classlib/binimp.h>
  8. #include <iostream.h>
  9. #include <strstrea.h>
  10. #include "../myclass.h" 
  11.  
  12. typedef TBinarySearchTreeImp<MyClass> ContainerType;
  13. typedef TBinarySearchTreeIteratorImp<MyClass> IteratorType;
  14.  
  15. const int MaxItems=6;
  16.  
  17. void ForEachCallBack(MyClass& mc, void *s)
  18. {
  19.     cout << (char *)s << mc << endl;
  20. }
  21.  
  22. void AddItems(ContainerType& container, int numItems)
  23. {
  24.     int multiplier = 1;
  25.     for( int i=numItems; i>0; i-- )
  26.         {
  27.         char buf[80];
  28.         ostrstream str(buf, sizeof(buf) );
  29.         str << (numItems-multiplier*i) << " hello" << ends;
  30.         multiplier = -1 * multiplier;
  31.         container.Add(MyClass(buf));
  32.         }
  33. }
  34.  
  35. void UseInorderIterator(ContainerType& container)
  36. {
  37.     IteratorType iterator(container);
  38.     while( iterator )
  39.         {
  40.         cout << iterator.Current() << endl;
  41.         iterator++;
  42.         }
  43. }
  44.  
  45. void UsePreorderIterator(ContainerType& container)
  46. {
  47.     IteratorType iterator(container, TBinarySearchTreeBase::PreOrder);
  48.     while( iterator )
  49.         {
  50.         cout << iterator.Current() << endl;
  51.         iterator++;
  52.         }
  53. }
  54.  
  55. void UsePostorderIterator(ContainerType& container)
  56. {
  57.     IteratorType iterator(container, TBinarySearchTreeBase::PostOrder);
  58.     while( iterator )
  59.         {
  60.         cout << iterator.Current() << endl;
  61.         iterator++;
  62.         }
  63. }
  64.  
  65. int main()
  66. {
  67.     ContainerType container;
  68.     AddItems(container, MaxItems);
  69.   
  70.     cout << "--- Starting ForEach" << endl;
  71.     container.ForEach(ForEachCallBack, (void*)"FE ");
  72.   
  73.     cout << "--- Starting Iterator (inorder)" << endl;
  74.     UseInorderIterator(container);
  75.   
  76.     cout << "--- Starting Iterator (preorder)" << endl;
  77.     UsePreorderIterator(container);
  78.   
  79.     cout << "--- Starting Iterator (postorder)" << endl;
  80.     UsePostorderIterator(container);
  81.   
  82.     return 0;
  83. }
  84.